package com.taimi.newdemo.controller;

import java.io.IOException;
import java.nio.file.Paths;

import com.taimi.newdemo.service.OpenvpnService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/openvpn")
public class OpenvpnController {

    @Autowired
    private OpenvpnService openvpnService;

    @PostMapping("/generate")
    public ResponseEntity<FileSystemResource> getOpenvpnFile(String clientName)
            throws IOException, InterruptedException {
        String filepath = openvpnService.generateOpenvpnClient(clientName);
        return openvpnService.export(Paths.get(filepath).toFile());
    }
}
